package shuanfa.sort;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
//计数排序
public class CountingSort {
    public static void main(String[] args) {
        int[] arr=new int[]{3,4,6,7,2,7,2,8,1,1};
        int[] array = new int[80000000];
        for (int i = 0; i < 80000000; i++) {
            array[i]=(int)(Math.random()*8 +1);
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = simpleDateFormat.format(new Date());
        System.out.println("排序前时间：" + format);
        countingSort(array,1,9);
        //System.out.println(Arrays.toString(arr));
        String format1 = simpleDateFormat.format(new Date());
        System.out.println("排序后的时间：" + format1);
    }

    private static void countingSort(int[] arr,int min,int max) {
        int[] bucket = new int[max - min + 1];
        int sortIndex = 0;
        //存入bucket计数
        for (int i =0;i<arr.length;i++){
            bucket[arr[i] - min] ++;
        }
        //从bucket中取出
        for (int i = min; i < max +1;i++) {
            while (bucket[i -min] > 0){
                arr[sortIndex++] = i;
                bucket[i -min]--;
            }
        }
    }

}
